<?php
App::uses('AppController', 'Controller');
/**
 * Purchases Controller
 *
 * @property Purchase $Purchase
 */
class PurchasesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Purchase->recursive = 0;
		$this->set('purchases', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Purchase->exists($id)) {
			throw new NotFoundException(__('Invalid purchase'));
		}
		$options = array('conditions' => array('Purchase.' . $this->Purchase->primaryKey => $id));
		$this->set('purchase', $this->Purchase->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
            $this->Purchase->create();

            $this->Purchase->set ( $this->data );
            $this->Purchase->PurchaseItem->set ( $this->data );

            $this->data = Set::filter ( $this->data );
            $result = $this->Purchase->saveAll ( $this->request->data );

            $this->Purchase->saveField('date', date('Y-m-d H:i:s'));


            if ($result) {
                $this->Session->setFlash ( __ ( 'Added successful' ) );

                $this->redirect ( array (
                    'action' => 'index'
                ) );
            } else {
                $this->Session->setFlash(__('The purchase could not be saved. Please, try again.'));
            }
        }

        $items = $this->Purchase->Item->find('list');
        $brands = $this->Purchase->Item->Brand->find('list');
        $colors = $this->Purchase->Item->Color->find('list');
        $conditions = $this->Purchase->Item->Condition->find('list');
        $sizes = $this->Purchase->Item->Size->find('list');
        $suppliers = $this->Purchase->Supplier->find('list');
        $statuses = $this->Purchase->Supplier->Status->find('list');

        $Item = $this->loadModel('Item');
        $itemStatuses = $this ->Item->ItemStatus-> find('list', array('order' => 'ItemStatus.name asc'));
        $categories = $this->Purchase->Item->Category->find('list');
        /*$itemStatuses = $this ->consignement->Item->Itemstatus('list')*/
        $this->set(compact('items', 'suppliers', 'brands', 'colors', 'conditions', 'sizes','categories','statuses','itemStatuses'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Purchase->exists($id)) {
			throw new NotFoundException(__('Invalid purchase'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Purchase->save($this->request->data)) {
				$this->Session->setFlash(__('The purchase has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The purchase could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Purchase.' . $this->Purchase->primaryKey => $id));
			$this->request->data = $this->Purchase->find('first', $options);
		}
		$items = $this->Purchase->Item->find('list');
		$suppliers = $this->Purchase->Supplier->find('list');
		$this->set(compact('items', 'suppliers'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Purchase->id = $id;
		if (!$this->Purchase->exists()) {
			throw new NotFoundException(__('Invalid purchase'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Purchase->delete()) {
			$this->Session->setFlash(__('Purchase deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Purchase was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
